<html>
<head><meta charset="utf-8"><title>Sharing a non global allocator · t-libs/wg-allocators · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/index.html">t-libs/wg-allocators</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html">Sharing a non global allocator</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="197009665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197009665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197009665">(May 09 2020 at 19:59)</a>:</h4>
<p>Will allocator traits/APIs support stuff like having an allocator instance, and having multiple containers that all use that same allocator instance? (Like having 3 vecs all using a shared local stack allocator)?</p>



<a name="197009746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197009746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197009746">(May 09 2020 at 20:01)</a>:</h4>
<p>Yes, this is possible when your allocator can be shared (i.e. Uses a Cell or Atomics). Then <code>AllocRef</code> can be implemented on <code>&amp;MyAlloc</code></p>



<a name="197009877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197009877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197009877">(May 09 2020 at 20:04)</a>:</h4>
<p>I think I get what you mean. Thanks! Are there any places that have examples of using the unstable non global allocator stuff? Like what a container API would look like</p>



<a name="197010020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197010020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197010020">(May 09 2020 at 20:08)</a>:</h4>
<p>Oh I see some interesting related discussion to my earlier question here <a href="https://github.com/rust-lang/wg-allocators/issues/55" title="https://github.com/rust-lang/wg-allocators/issues/55">https://github.com/rust-lang/wg-allocators/issues/55</a></p>



<a name="197010024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197010024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197010024">(May 09 2020 at 20:08)</a>:</h4>
<p>Would the changes mentioned in that issue mean that just implementing AllocRef for MyAlloc would be enough? (Instead of for &amp;MyAlloc)</p>



<a name="197011584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197011584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197011584">(May 09 2020 at 20:34)</a>:</h4>
<p>I'm currently not at my PC but you might want to take a look at the <code>alloc-wg</code> crate and the <code>alloc-compose</code> crate. Disclaimer: both are from me and at an experimental state but you should be able to get the idea how the trait is designed.</p>
<p>I'll take a look at the issue tomorrow :)</p>



<a name="197011916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197011916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197011916">(May 09 2020 at 20:42)</a>:</h4>
<p>Those are exactly what I was looking for, ty</p>



<a name="197012079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197012079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197012079">(May 09 2020 at 20:46)</a>:</h4>
<p>I'm currently not at my PC but you might want to take a look at the <code>alloc-wg</code> crate and the <code>alloc-compose</code> crate. Disclaimer: both are from me and at an experimental state but you should be able to get the idea how the trait is designed.</p>
<p>I'll take a look at the issue tomorrow :)</p>



<a name="197012092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197012092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197012092">(May 09 2020 at 20:47)</a>:</h4>
<p>Is runtime dispatch considered by any of those? I feel like ergonomics of generics all over for allocators for code using containers might be annoying. Or would it be fine to just use an impl for a dyn Alloc?</p>



<a name="197041921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197041921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197041921">(May 10 2020 at 08:31)</a>:</h4>
<p>You are talking about <code>alloc-compose</code>? You could still use aliases for nested allocators.</p>



<a name="197163266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197163266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197163266">(May 11 2020 at 15:35)</a>:</h4>
<p>I was thinking more like, say you're writing a function or a type that needs to use a Vec, it would probably be less painful to not have to be generic over an A: AllocRef, and it would also not need monomorphization (Which leads to compile time and machine code size bloat). Is there a way to do that?</p>



<a name="197163603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197163603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197163603">(May 11 2020 at 15:37)</a>:</h4>
<p>No. If you don't want to modify the vector you should pass a slice anyway, but if you want to push elements, for example, it is required, that the vector knows, how to allocate additional space. Currently, there is no implementation for <code>Box&lt;A: AllocRef&gt;</code> or similar, but those will probably be added later.</p>



<a name="197187897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197187897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197187897">(May 11 2020 at 18:48)</a>:</h4>
<p>Yes but I thought you could have some kind of runtime dispatch (Like a Box&lt;dyn AllocRef&gt;) so that you could handle the same non generic function Vec's that use different allocators</p>



<a name="197188140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197188140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197188140">(May 11 2020 at 18:50)</a>:</h4>
<p>the cppcon andrei talk linked in the alloc-compose docs was great</p>



<a name="197501092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Sharing%20a%20non%20global%20allocator/near/197501092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christopher Durham <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Sharing.20a.20non.20global.20allocator.html#197501092">(May 14 2020 at 02:41)</a>:</h4>
<p>The short of it is that some sort of <code>DynAlloc</code> definitely is needed. Right now, that trait is <code>GlobalAlloc</code>. Note that nothing about the <code>GlobalAlloc</code> trait _requires_ the allocator be used as a global allocator. Instead, assuming we have <code>impl AllocRef for &amp;'_ dyn GlobalAlloc</code>, you could use <code>Box&lt;T, &amp;dyn GlobalAlloc&gt;</code> just fine. (Or <code>&amp;dyn DynAlloc</code> if we decide to replace <code>GlobalAlloc</code> wholesale, or <code>&amp;dyn AllocRef</code> if <code>AllocRef</code> ends up being object-safe.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>